package net.yambal.android.secretary_alarm.db;

import net.yambal.android.secretary_alarm.db.Alarm;
import net.yambal.android.util.ArrayUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class AlarmsTable {
	private Context _context;
	private DatabaseHelper _db_helper;
	private SQLiteDatabase _db;
	public static final String TABLE = "Alarms";
	
	public AlarmsTable (Context context){
		_context = context.getApplicationContext();
		_db_helper = DatabaseHelper.getInstance(context);
	}
	
	public void save(Alarm alarm){
		this.open();
		
		ContentValues values = new ContentValues();
		values.put("label", alarm.label);
		values.put("days", ArrayUtil.ListToCSV(alarm.days));
		values.put("hour", alarm.hour);
		values.put("minute", alarm.minute);
		
		if(alarm.id < 0){
			// insert
			_db.insert(TABLE, null, values);
		}else{
			// update
			_db.update(TABLE, values, "_id = " + alarm.id, null);
		}
		this.close();
	}
	
	public void delete(Alarm alarm){
		this.open();
		Log.d("sa", "delete@AlarmsTable " + alarm.label + "("+ alarm.id +")");
		if(alarm != null && alarm.id >=0){
			_db.execSQL("DELETE FROM " + TABLE + " WHERE _id = " + alarm.id + ";");
		}
		this.close();
	}
	
	public Alarm get(Long id){
		open();
		Cursor cursor = null;
		cursor = _db.query(TABLE, new String[] {"_id","label","days","hour","minute"}, "_id = " + String.valueOf(id), null, null, null, null);

		Alarm alarm;
		if (cursor != null && cursor.moveToFirst()) {
			cursor.moveToFirst();
			alarm = new Alarm(_context);
			alarm.id = cursor.getLong(0);
			alarm.label = cursor.getString(1);
			//alarm.days = cursor.getString(2).split(",");
			alarm.days = ArrayUtil.CSVToList(cursor.getString(2));
			alarm.hour = cursor.getInt(3);
			alarm.minute = cursor.getInt(4);
		}else{
			alarm = null;
		}
		if (cursor != null){
			cursor.close();
		}
		close();
		return alarm;
	}
	public Alarm[] list(){
		open();
		Alarm[] alarms;
		
		Cursor cursor;
		cursor = _db.query(TABLE, new String[] {"_id","label","days","hour","minute"}, null, null, null, null, null);
		
		int numRows = 0;
		if (cursor != null && cursor.moveToFirst()) {
			numRows = cursor.getCount();
			alarms = new Alarm[numRows];
			
			for(int i = 0; i<numRows; i++){
				Alarm alarm = new Alarm(_context);
				alarm.id = cursor.getLong(0);
				alarm.label = cursor.getString(1);
				//alarm.days = cursor.getString(2).split(",");
				alarm.days = ArrayUtil.CSVToList(cursor.getString(2));
				alarm.hour = cursor.getInt(3);
				alarm.minute = cursor.getInt(4);
				alarms[i] = alarm;
				cursor.moveToNext();
			}
		}else{
			alarms = new Alarm[0];
		}
		
		if (cursor != null){
			cursor.close();
		}
		close();
		
		Log.d("sa", "list@AlarmsTable " + String.valueOf(numRows));
		return alarms;
	}
	
	public void open(){
		//Log.d("sa", "open@AlarmsTable ");
		if(_db_helper != null){
			_db = _db_helper.getWritableDatabase();
		}else{
			_db = null;
		}
	}
	
	public void close() {
		//Log.d("sa", "close@AlarmsTable ");
		_db.close();
	}
}
